package com.cskaoyan.market.admin.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.cskaoyan.market.admin.service.MarketAdminCouponService;
import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.db.domain.MarketCouponExample;
import com.cskaoyan.market.db.domain.MarketCouponUser;
import com.cskaoyan.market.db.domain.MarketCouponUserExample;
import com.cskaoyan.market.db.mapper.MarketCouponMapper;
import com.cskaoyan.market.db.mapper.MarketCouponUserMapper;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;

/**
 * @author James
 * @since 2023/03/21 23:14
 */

public class MarketAdminCouponServiceImpl implements MarketAdminCouponService {
    @Override
    public List<MarketCouponUser> getUserListByCouponId(int page, int limit, Integer couponId, String sort,
                                                        String order) {
        SqlSession session = MyBatisUtil.getSession();
        MarketCouponUserMapper mapper = session.getMapper(MarketCouponUserMapper.class);
        MarketCouponUserExample marketCouponUserExample = new MarketCouponUserExample();
        marketCouponUserExample.or().andCouponIdEqualTo(couponId);
        marketCouponUserExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        List<MarketCouponUser> marketCouponUsers = mapper.selectByExample(marketCouponUserExample);
        session.commit();
        session.close();
        return marketCouponUsers;
    }

    @Override
    public MarketCoupon update(int days, boolean deleted, Double discount, int goodsType, List<Integer> goodsValue,
                               int id, Integer limit, String min, String name, Integer status, String tag, Integer timeType, Integer total,
                               Integer type, String updateTime) {
        SqlSession session = MyBatisUtil.getSession();
        MarketCouponMapper mapper = session.getMapper(MarketCouponMapper.class);

        MarketCoupon marketCoupon = mapper.selectByPrimaryKey(id);
        // 字符串类型的转化为localTime ,输出
        marketCoupon.setDays(Short.parseShort(Integer.toString(days)));
        marketCoupon.setDeleted(deleted);
        marketCoupon.setDiscount(BigDecimal.valueOf(discount));
        marketCoupon.setGoodsType((short) goodsType);
        // marketCoupon.setGoodsValue(goodsValue);
        marketCoupon.setId(id);
        // marketCoupon.setLimit(limit);

        mapper.updateByPrimaryKey(marketCoupon);
        session.commit();
        session.close();
        return marketCoupon;
    }

    @Override
    public void delete(int days, boolean deleted, Double discount, int goodsType, List<Integer> goodsValue, int id,
                       Integer limit, String min, String name, Integer status, String tag, Integer timeType, Integer total,
                       Integer type, String updateTime) {
        SqlSession session = MyBatisUtil.getSession();
        MarketCouponMapper mapper = session.getMapper(MarketCouponMapper.class);
        MarketCoupon marketCoupon = mapper.selectByPrimaryKey(id);
        marketCoupon.setUpdateTime(LocalDateTime.now());
        marketCoupon.andLogicalDeleted(true);
        marketCoupon.setDeleted(true);

        mapper.updateByPrimaryKey(marketCoupon);
        session.commit();
        session.close();
    }

    @Override
    public List<MarketCoupon> list(int page, int limit, String name, String type, String status, String sort,
                                   String order) {

        SqlSession session = MyBatisUtil.getSession();
        MarketCouponMapper couponMapper = session.getMapper(MarketCouponMapper.class);

        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = marketCouponExample.createCriteria();
        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtil.isEmpty(status)) {
            criteria.andStatusEqualTo(Short.valueOf(status));
        }
        if (!StringUtil.isEmpty(type)) {
            criteria.andTypeEqualTo(Short.valueOf(type));
        }
        criteria.andDeletedEqualTo(false);
        // 设置排序规则
        marketCouponExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        List<MarketCoupon> couponList = couponMapper.selectByExample(marketCouponExample);
        session.commit();
        session.close();
        return couponList;
    }
}